//
//  AttendanceViewController.m
//  ZYShang
//
//  Created by wangguangming on 15/7/31.
//  Copyright © 2015年 Amber. All rights reserved.
//

#import "AttendanceViewController.h"
#import "ReadyDiSignle.h"
#import "UserModel.h"
#import "AttendanceView.h"
#import "Base.h"
#import "UIImage+imagedown.h"
#import "Loadsingle.h"
@interface AttendanceViewController ()
{
    AttendanceView *_itemView;
    UIScrollView *_mainScroll;
    int _row;
}
@end

@implementation AttendanceViewController
-(void)viewWillAppear:(BOOL)animated
{
    
    [super viewWillAppear:animated];
    UserModel *userModel=[Loadsingle single].userArray[0];
    [ReadyDiSignle signle].oidString=userModel.mktId;
    [[ReadyDiSignle signle] getAllwokers];
     
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setBack];
    self.view.backgroundColor=COLOR;
    self.title=@"出勤管理";
    _mainScroll=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    [self.view addSubview:_mainScroll];
    _mainScroll.backgroundColor=COLOR;
    [[ReadyDiSignle signle] addObserver:self forKeyPath:@"workerArray" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];  
}


-(void)observeValueForKeyPath:(nullable NSString *)keyPath ofObject:(nullable id)object change:(nullable NSDictionary *)change context:(nullable void *)context
{
    if ([keyPath isEqualToString:@"workerArray"]) 
        {
        
        [self shuju];
               
        [[ReadyDiSignle signle] removeObserver:self forKeyPath:@"workerArray"];
        }
    if ([keyPath isEqualToString:@"noticeArray"]) 
        {
        
            //[ReadyDiSignle signle].noticeArray;
       [[ReadyDiSignle signle] removeObserver:self forKeyPath:@"noticeArray"]; 
      NSDictionary *ss=  [ReadyDiSignle signle].noticeArray[0];
        NSString *s=[ss valueForKey:@"msg"];
        if (s.length<5) 
            {
            
                    
            }
        else
            {
            UIButton *button=(UIButton *)[_mainScroll viewWithTag:_row];
            [button setTitle:@"下班打卡" forState:UIControlStateNormal];
            button.backgroundColor=[UIColor blueColor];

            UIAlertView *a=[[UIAlertView alloc] initWithTitle:@"提醒" message:s delegate:self cancelButtonTitle:@"YES" otherButtonTitles:@"NO", nil];
            [a show];
           
            }
        
       }
}
-(void)shuju
{
    
    for (int i=0; i<[ReadyDiSignle signle].workerArray.count; i++)
        {
        UserModel *user=[ReadyDiSignle signle].workerArray[i];
        _itemView=[[AttendanceView alloc] initWithFrame:CGRectMake(5+(WIDTH/2-5)*(i%2), 5+((WIDTH/2-10)+130)*(i/2), WIDTH/2-10, WIDTH/2-10+125)];
        _itemView.backgroundColor=[UIColor whiteColor];
        if (user.photo==nil)
            {
             _itemView.photoImage.image=[UIImage imageNamed:@"default_userhead.jpg"];
         }
      else  {
          
        _itemView.photoImage.image=[UIImage imageDownWithurlstring:user.photo imagebolock:^(UIImage *img) {
            _itemView.photoImage.image=img;
        }];
        }
        _itemView.nameLaner.text=user.name;
        _itemView.idLaber.text=user.account;
        _itemView.phoneLaber.text=user.tel;
        
        if (user.checked.intValue==0) 
            {
             [_itemView.offButton setTitle:@"上班签到" forState:UIControlStateNormal];
             _itemView.offButton.backgroundColor=[UIColor redColor];
            }
        else
            {
             [_itemView.offButton setTitle:@"下班打卡" forState:UIControlStateNormal];
             _itemView.offButton.backgroundColor=[UIColor blueColor];
            }
       
        _itemView.typeLaber.text=[self setType:user.type.integerValue];
       
        [_itemView.offButton setTag:110+i];
        [_itemView.offButton  addTarget:self action:@selector(offAction:) forControlEvents:UIControlEventTouchUpInside];
        
        
        
        [_mainScroll addSubview:_itemView];
       }
    _mainScroll.contentSize=CGSizeMake(0, ((WIDTH/2-10)+130)*([ReadyDiSignle signle].workerArray.count/2+[ReadyDiSignle signle].workerArray.count%2));
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/**
 public static class Type {
 public static final Integer FULLTIME = 1; // 全职
 public static final Integer PARTTIME = 2; // 兼职
 public static final Integer INTERN = 3; // 实习生
 public static final Integer TRIAL = 4; // 试用期
 }
 */

-(NSString *)setType:(int )integer
{
    if (integer==1)
        {
        return @"全职";
       }
    else if (integer==2)
        {
        return @"兼职";
        }
    else if (integer==3)
        {
        return @"实习生";
        }
    else
        {
        return @"实习期";
        }
}
-(void)offAction:(UIButton *)sender
{
    
     UserModel *user=[ReadyDiSignle signle].workerArray[sender.tag-110];
    _row=sender.tag;
    
   if([sender.titleLabel.text isEqualToString:@"上班签到"])
       {
       [ReadyDiSignle signle].oidString=user._id;
       [[ReadyDiSignle signle] onwork];
       UIButton *button=(UIButton *)[_mainScroll viewWithTag:sender.tag];
       [button setTitle:@"下班打卡" forState:UIControlStateNormal];
       button.backgroundColor=[UIColor blueColor];
       
      
       }
    else if([sender.titleLabel.text isEqualToString:@"下班打卡"])
        {
        [ReadyDiSignle signle].oidString=user._id;
        [[ReadyDiSignle signle] offwork];
        
        [[ReadyDiSignle signle] addObserver:self forKeyPath:@"noticeArray" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
       
        UIButton *button=(UIButton *)[_mainScroll viewWithTag:_row];
        [button setTitle:@"上班签到" forState:UIControlStateNormal];
        button.backgroundColor=[UIColor redColor];
        

        }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
